<template>
    <div class="top leaderEmail">
        <div class="email_email">
          <div class="email_bg">
            <div class="email_logo flex flex-align-center ">
              <img src="../../assets/img/email_logo.png">
              <p>院长、政委信箱</p>
            </div>
          </div>
        </div>
        <div class="dqwz clearfix">
          <div class="wz fl">
            <span>当前位置</span>
            <span>></span>
            <span><a href="/home">首页</a></span>
            <span>></span>
            <span>院长、政委信箱</span>
          </div>
        </div>

        <div class="email_cont clearfix">
          <div class="email_list fl">
            <h1>信件列表</h1>
            <div class="email_search flex flex-align-center flex-pack-justify">
              <div class="select_list flex flex-align-center">
                  <span>筛选：</span>
                  <ul class="clearfix">
                    <li v-for="(item,index) in select_list"  :class="[status==index ? 'on' : '']" @click="selectItem(index)">{{item.label}}</li>
                  </ul>
              </div>
              <div class="select_search flex flex-align-center">
                <el-input v-model="inputValue" placeholder="请输入关键字"></el-input>
                <el-button type="danger" style="margin-left:15px" @click="goSearch">查询</el-button>
              </div>
            </div>
            <div class="email_data">
              <ul v-if="dataList.length > 0">
                <li v-for="(item,index) in dataList">
                  <h2 v-if="item.ask.shifouhuifu == '未回复'" class="ys">{{item.ask.title}}</h2>
                  <h2 v-if="item.ask.shifouhuifu != '未回复'">{{item.ask.title}}</h2>
                  <div class="time flex" >
                    <div><i class="iconfont icon-shijian"></i>{{item.ask.liuyanshijian}}</div>
                    <div style="margin-left:30px;">留言人：{{item.ask.liuyanren}}</div>
                    <div style="margin-left:30px;">留言科室：{{item.ask.liuyanrendanwei}}</div>
                  </div>
                  <div class="answer">{{item.ask.content}}</div>
                  <div class="reply" v-for="(item2,index2) in item.replylist">
                    <span>{{item2.bumen}}：</span>{{item2.replycontent}}
                  </div>
                  <!-- <div class="btn"><el-button type="danger">回复</el-button></div> -->
                </li>
              </ul>
              <el-empty v-if="dataList.length <= 0" description="暂无数据"></el-empty>               
            </div>

            <div v-if="dataList.length <= 0" class="page" style="text-align:center;margin-top:30px;">
                <el-pagination
                  :small="true"
                  :background="true"
                  @current-change="handleSizeChange"
                  :current-page="pageindex" 
                  :page-size="pagesize" 
                  layout="total,prev, pager, next,jumper"
                  :total="retotal"
                  prev-text="<上一页"
                  next-text="下一页>"
                  >
                </el-pagination>  
              </div>

          </div>
          <div class="my_message fr">
            <div class="xuzhi">
              <p>留言须知：</p>
              <p>1.信箱留言应严格遵守法律法规及社会公德，不得捏造、歪曲事实；不得诋毁、辱骂他人；不得煽动民族、宗教、种族、地域等敏感问题；不得散布淫秽、色情、暴力等不良信息。对于违反法律法规的留言行为，留言者自行承担相应的法律责任。 </p>
              <p>2.信箱留言人应当自觉遵守互联网礼仪，进行理性、真诚、友善的互动交流，不得发表人身攻击等激化矛盾的言论；不得进行恶意刷屏等破坏秩序的行为，同时，应当尊重他人的合法权益，不得侵犯他人的隐私、名誉等。</p>
              <p>3.领导信箱是医院官兵员工向院领导提出意见和建议的渠道之一，对于所反映问题，建议首先与医院相关部门联系沟通，了解实际情况，并寻求解决措施。 </p>
              <p>4.为便于及时准确地了解留言诉求，建议准确详细地描述所反映的问题，并根据各职能部门职责范围，@相应部门进行留言，以便有针对性地解决您反映的问题。留言成功后，所涉部门会及时接收，按规定报送、处理并原则上于三日内反馈处理情况。在此期间，请不要重复提交。</p>
            </div>
            <div class="form">
              <el-form ref="form" :model="form" label-width="80px" label-position="top">
                <!-- <el-row :gutter="20"> -->
                  <!-- <el-col :span="12">
                    <el-form-item label="昵称">
                      <el-input></el-input>
                    </el-form-item>
                  </el-col> -->
                  <!-- <el-col :span="12">
                    <el-form-item label="联系方式">
                      <el-input></el-input>
                    </el-form-item>
                  </el-col> -->
                <!-- </el-row> -->
                <el-form-item label="标题">
                  <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                  <el-input v-model="form.content" type="textarea" :rows="6"></el-input>
                </el-form-item>
                <el-row style="padding-top:20px">
                  <el-col :span="12">
                    <el-form-item>
                      <el-checkbox v-model="form.shifouyinsi">是否隐私</el-checkbox>
                    </el-form-item>  
                  </el-col>  
                  <el-col :span="12" style="text-align:right">
                    <el-form-item>
                      <el-button type="danger" @click="submit('form')">发表</el-button>
                    </el-form-item>  
                  </el-col>
                </el-row>  
              </el-form>  
            </div>

          </div>
        </div>

        <el-dialog title="登录" :visible.sync="dialogVisible" width="500px">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <el-form-item prop="username" style="margin-bottom:20px">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="账号"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="user"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="password" style="margin-bottom:20px">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="password"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled" style="margin-bottom:20px">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="validCode"
                  class="el-input__icon input-icon"
                />
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-checkbox
              v-model="loginForm.rememberMe"
              style="margin: 0px 0px 25px 0px"
              >记住密码</el-checkbox
            >
            <el-form-item style="width: 100%">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width: 100%"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right">
                <router-link class="link-type" :to="'/register'"
                  >立即注册</router-link
                >
              </div>
            </el-form-item>
          </el-form>
        </el-dialog>  

        <footIndex />
    </div>    
</template> 
<style rel="stylesheet/scss" lang="scss">
// .login {
//   display: flex;
//   justify-content: center;
//   align-items: center;
//   height: 100%;
//   background-image: url("../assets/images/login-background.jpg");
//   background-size: cover;
// }
// .title {
//   margin: 0px auto 30px auto;
//   text-align: center;
//   color: #707070;
// }

.login-form {
  border-radius: 6px;
  background: #ffffff;
  // width: 400px;
  // padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
<style lang="scss" scoped>
::v-deep.el-form--label-top .el-form-item__label{
   padding: 0px;
}
::v-deep.el-form-item{
  margin-bottom: 0px;
}
::v-deep.el-input--medium .el-input__inner{
   height: 42px;
   line-height: 42px;
   background: #f9f9f9;
}
::v-deep.el-textarea__inner{
  background-color: #f9f9f9;
}
//未选中框
::v-deep .el-checkbox__inner:hover{
  border-color: #c0000d;
}
  //选中框
::v-deep .el-checkbox__input.is-checked .el-checkbox__inner{
  background-color: #c0000d;
  border-color: #c0000d;
}
::v-deep .el-checkbox__input.is-focus .el-checkbox__inner{
  // background-color: #c0000d;
  border-color: #c0000d;
}
  //未选中框时右侧文字
::v-deep .el-checkbox__label{

}
  //选中框时右侧文字
::v-deep .el-checkbox__input.is-checked+.el-checkbox__label{
  color: #c0000d;
}
::v-deep.el-button--danger{
  background-color: #c0000d;
  border-color: #c0000d;
}
::v-deep .el-button--danger:hover, .el-button--danger:focus{
  background-color: #c0000d;
  border-color: #c0000d;
}
</style>   
<script setup name="email">
import footIndex from "./components/footEmail/index.vue";
import { getEmail, sendEmail } from "@/api/yxzx/email";
import { login, getCodeImg, loginagain } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
export default {
  components: {
    footIndex,
  },
  data() {
    return {
      form:{
        zhonglei: '',
      },
      checked: false,
      select_list: [
        {key:0,label:'全部'},
        {key:0,label:'已回复'},
        {key:0,label:'未回复'},
        // {key:0,label:'私密信件'}
      ],
      status: 0,
      inputValue: '',
      pageindex: 1,
      pagesize: 6,
      queryParams:{
        pageindex: 1,
        pagesize: 10,
        ishuifu: 2,
        neirong: '',
      },
      retotal: 0,
      dataList: [],
      dialogVisible: false,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    // 筛选操作
    selectItem(index){
        this.status = index
        this.queryParams.pageindex = 1
        if(index==0){
          this.queryParams.ishuifu = 2
        } else if(index==1){
          this.queryParams.ishuifu = 1
        } else if(index==2){
          this.queryParams.ishuifu = 0
        }
        this.getData()
    },
    // 分页操作
    handleSizeChange(val){
      this.queryParams.pageindex = val
      this.getData()
    },
    // 查询按钮操作
    goSearch(){
      if(this.inputValue==''){
        this.$message.error('请输入关键字！');
        return
      }
      this.queryParams.pageindex = 1
      this.queryParams.neirong = this.inputValue
      this.getData()
    },
    // 查询邮箱数据
    getData(){
      getEmail(
        this.queryParams.pageindex,
        this.queryParams.pagesize,
        this.queryParams.ishuifu,
        this.queryParams.neirong,
        ).then((response) => {
          this.dataList = response.resultContent.AskList
          this.retotal = response.resultContent.PageInfo.recordCount
        // console.log(response)
      });
    },
    // 发表留言
    submit(name){
      if(!this.form.content || this.form.content==''){
        this.$message.error('请填写留言内容！');
        return
      }
      if(this.form.shifouyinsi == true){
        this.form.shifouyinsi = '1'
      } else {
        this.form.shifouyinsi = '0'
      }
      sendEmail(
        this.form.shifouyinsi,
        this.form.title,
        this.form.content,
        this.form.zhonglei
        ).then((response) => {
          if(response.result == '0' && response.message == '请先登录！'){
            this.$confirm('您还没有登录，登录后才能发表留言！', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.dialogVisible = true
              this.getCode();
              this.getCookie();
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消登录！'
              });          
            });           
          } else if (response.result == '1'){
              this.$message({
                message: '留言发表成功！',
                type: 'success'
              });
              this.form.title = ''
              this.form.content = ''
              this.$refs['form'].resetFields();
              this.queryParams.pageindex = 1
              this.getData()
          }
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              this.dialogVisible = false
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  }
}    
</script>